//
// Created by wangyihu2020 on 2022/6/24.
//

#include <Sparse_Matrix.h>
#include <iostream>
#include <CSC_Matrix.h>
#include <CSR_Matrix.h>
#include <window.h>
qp_int Sparse_Matrix::getDataSize() const{
    return m_dataSize;
}

qp_int Sparse_Matrix::getColSize() const{
    return m_colSize;
}

qp_int Sparse_Matrix::getRowSize() const {
    return m_rowSize;
}

qp_float *Sparse_Matrix::getData() const{
    return m_data;
}

void Sparse_Matrix::printData() const {
    std::cout<<"Data array( size: "<<m_dataSize << ") :"<<std::endl;
    for(int i = 0; i < m_dataSize; i++){
        std::cout<<m_data[i]<<' ';
//        if(i % 10 == 9) std::cout<<std::endl;
    }
    std::cout<<std::endl;
}

volatile void Sparse_Matrix::printAll() const {
    printCol();
    printRow();
    printData();
    std::cout<<std::endl;
}

Sparse_Matrix::~Sparse_Matrix() {
    free(m_row);
    free(m_col);
    free(m_data);
}

qp_int *Sparse_Matrix::getRow() const {
    return m_row;
}

qp_int *Sparse_Matrix::getCol() const {
    return m_col;
}

void Sparse_Matrix::setDataSize(qp_int data) {
    m_dataSize = data;
}

void Sparse_Matrix::setColSize(qp_int data) {
    m_colSize = data;
}

void Sparse_Matrix::setRowSize(qp_int data) {
    m_rowSize = data;
}

void Sparse_Matrix::writeData(qp_int index, qp_float data) {
    m_data[index] = data;
}

void Sparse_Matrix::writeRow(qp_int index, qp_int data) {
    m_row[index] = data;
}

void Sparse_Matrix::writeCol(qp_int index, qp_int data) {
    m_col[index] = data;
}

Sparse_Matrix *Sparse_Matrix::transpose(Sparse_Matrix * mat) {
    return transpose(*mat);
}

Sparse_Matrix *Sparse_Matrix::transpose(Sparse_Matrix& mat) {
    Sparse_Matrix* res = nullptr;
    if(mat.m_type == CSR_MATRIX)  res = new CSC_Matrix(mat.getColSize(), mat.getRowSize());
    else if(mat.m_type == CSC_MATRIX) res = new CSR_Matrix(mat.getColSize(), mat.getRowSize());
    else{
        std::cout<<"error in sparse_matrix transpose"<<std::endl;
        return nullptr;
    }
    res->m_row = mat.m_col;
    res->m_col = mat.m_row;
    res->m_data = mat.m_data;
    res->setDataSize(mat.getDataSize());
    return res;
}